﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinFu.Reflection;

namespace Hiro.Loaders
{
    /// <summary>
    /// Represents a loader that can be configured to load any type from an assembly.
    /// </summary>
    /// <typeparam name="TTarget">The target type that will be loaded.</typeparam>
    internal class CustomLoader<TTarget>
    {
        private readonly TTarget _target;
        private IAssemblyLoader _assemblyLoader;
        private readonly Loader<TTarget> _loader = new Loader<TTarget>();

        public CustomLoader(TTarget target, IAssemblyLoader assemblyLoader, params IActionLoader<TTarget, Type>[] typeLoaders)
        {
            _target = target;
            _assemblyLoader = assemblyLoader;

            var assemblyTargetLoader = new AssemblyTargetLoader<TTarget>();
            assemblyTargetLoader.AssemblyLoader = assemblyLoader;

            foreach (var actionLoader in typeLoaders)
            {
                assemblyTargetLoader.TypeLoaders.Add(actionLoader);
            }

            _loader.FileLoaders.Add(assemblyTargetLoader);
        }

        public void LoadFrom(string directory, string filespec)
        {
            _loader.LoadDirectory(directory, filespec);
            _loader.LoadInto(_target);
        }
    }
}
